<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        
        th,
        td {
            padding: 8px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        
        tr:hover {
            background-color: #f5f5f5;
        }
        
        th {
            background-color: #4CAF50;
            color: white;
        }
        
        .editBox,
        .addnoteBox {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #EEE;
            padding: 50px;
            display: none;
        }
        
        .updatebtn {
            float: right;
        }
    </style>
</head>

<body>
    <button class="addnoteBtn">添加笔记</button>
    <p>
        <input type="text" name="" id="searchinp">
        <button class="searchbtn">搜索</button>
    </p>

    <table class="table">
    </table>
    <p></p>

    <div class="editBox">
        <input type="hidden" name="id" id="noteid"> <br />
        <label>标题</label><input type="text" name="title" id="notetitle"><br />
        <label>内容</label><textarea name="content" id="notecontent" cols="30" rows="10"></textarea><br />
        <input type="button" name="" id="" value="更新" class="updatebtn" onclick="updatepost()" />
    </div>

    <div class="addnoteBox">
        <input type="hidden" name="id" id="noteid"> <br />
        <label>标题</label><input type="text" name="title" id="addnotetitle"><br />
        <label>内容</label><textarea name="content" id="addnotecontent" cols="30" rows="10"></textarea><br />
        <input type="button" name="" id="" value="添加" class="updatebtn" onclick="addNotepost()" />
    </div>
    <script src="./js/request.js"></script>
    <script>
        let editBox = document.querySelector('.editBox');
        let noteId = document.querySelector('#noteid');
        let noteTitle = document.querySelector('#notetitle');
        let noteContent = document.querySelector('#notecontent');
        let updateBtn = document.querySelector('.updatebtn');
        let addnoteBtn = document.querySelector('.addnoteBtn');
        let addnoteBox = document.querySelector('.addnoteBox');
        let addnotetitle = document.querySelector('#addnotetitle')
        let addnotecontent = document.querySelector('#addnotecontent')
        let searchbtn = document.querySelector('.searchbtn');
        let searchinp = document.querySelector('#searchinp')
        let table = document.querySelector('.table');

        // 搜索事件
        searchbtn.onclick = async function() {
            let searchstr = searchinp.value;
            let {
                data
            } = await noteAxios(`/searchnote?str=${searchstr}`);

            // console.log(data);
            let str = data.map(item => `
                    <tr>
                        <td>${item.title}</td>
                        <td><a href="javascript:;" onclick="showupdate('${item._id}')">编辑</a></td>
                        <td><a href="javascript:;" onclick="deletenote('${item._id}')">删除</a></td>
                    </tr>
                `).join('');
            // 填入到表格标签中
            table.innerHTML = str
        }

        // 显示添加笔记模态框
        addnoteBtn.onclick = function() {
            addnoteBox.style.display = 'block';
        }

        // 发送添加笔记请求事件
        function addNotepost() {
            // console.log("发送了");
            noteAxios({
                url: "addNote",
                method: "post",
                data: {
                    title: addnotetitle.value,
                    content: addnotecontent.value
                }
            }).then(val => {
                console.log(val);
                if (val.code === 2000) {
                    window.location.reload();
                }
            })
        }

        // 获取笔记列表
        noteAxios('/noteList?page=0&limit=8').then(({
            data,
            code,
            message
        }) => {
            // 如果数据的code是2000 就是获取成功。
            if (code === 2000) {
                //遍历数据 - 将数据依次加入到tr中
                let str = data.map(item => `
                    <tr>
                        <td>${item.title}</td>
                        <td><a href="javascript:;" onclick="showupdate('${item._id}')">编辑</a></td>
                        <td><a href="javascript:;" onclick="deletenote('${item._id}')">删除</a></td>
                    </tr>
                `).join('');
                // 填入到表格标签中
                table.innerHTML = str
            } else {
                // 如果不是2000 - 说明获取数据失败
                document.querySelector('p').innerHTML = message;
            }
        })

        // 点击编辑按钮 = 添加编辑回显功能
        function showupdate(id) {
            // console.log('点了');
            // noteAxios(`/detailNote?id=${id}`)
            noteAxios({
                url: "/detailNote",
                params: {
                    id
                }
            }).then(val => {
                console.log(val);
                if (val.code === 2000) {
                    // 显示模态框
                    editBox.style.display = 'block';
                    // 数据回填
                    noteId.value = val.data._id;
                    noteTitle.value = val.data.title;
                    noteContent.value = val.data.content;

                }
            })
        }

        // 提交编辑- 更新事件
        function updatepost() {
            // console.log('执行了');
            // 需要获取你修改的id值
            let id = noteId.value;
            let title = noteTitle.value;
            let content = noteContent.value;
            // console.log(id);

            noteAxios({
                url: "/updateNote",
                method: "post",
                data: {
                    id,
                    title,
                    content
                }
            }).then(val => {
                console.log(val);

                if (val.code === 2000) {
                    window.location.reload();
                }
            })

        }

        // 删除笔记
        async function deletenote(id) {
            let {
                code
            } = await noteAxios(`/deleteNote?id=${id}`);

            // console.log(data);
            if (code === 2000) {
                window.location.reload();
            } else {
                alert('抱歉删除失败');
            }
        }
    </script>
</body>

</html>